﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class AddMenuCategoryDialog : ChildWindow
    {

        public AddMenuCategoryDialog()
        {
            InitializeComponent();
            init();
        }

        private async void init()
        {
            cmbMenuGroup.Items.Clear();
            await Globals.db.GetMenuGroupObjects("Active=1 order by Name");
            if (Globals.db.MenuGroupRecords.Count() > 0)
            {
                foreach (ServiceReferenceOpenRPOS.MenuGroup mg in Globals.db.MenuGroupRecords)
                {
                    cmbMenuGroup.Items.Add(mg.col_Name);
                }
            }
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Get the top precedence number 
            short maxPrecedence=0;
            foreach (ServiceReferenceOpenRPOS.MenuCategory mc in Globals.db.MenuCategoryRecords)
            {
                if (mc.col_Precedence > maxPrecedence)
                    maxPrecedence = mc.col_Precedence;
            }

            if (tbMenuCategoryName.Text.Trim().Length>0)
            {
                // Determine if a non-active record with this name already exists
                await Globals.db.GetMenuCategoryObjects("Active=0 and Name='" + tbMenuCategoryName.Text.Trim() + "'");
                if (Globals.db.MenuCategoryRecords.Count > 0)
                {
                    if (tbCategoryPrintroute.Text.Trim().Length > 0)
                    {
                        if (Convert.ToInt32(tbCategoryPrintroute.Text.Trim()) > 255)
                        {
                            MessageBox.Show("Print Route value should be between 0 - 255");
                        }
                        else
                        {
                            Globals.db.MenuCategoryRecords[0].col_Active = true;
                            Globals.db.MenuCategoryRecords[0].bFilterActive = true;
                            Globals.db.MenuCategoryRecords[0].col_Name = tbMenuCategoryName.Text.Trim();
                            Globals.db.MenuCategoryRecords[0].bFilterName = true;
                            Globals.db.MenuCategoryRecords[0].col_PrintRoute = Convert.ToByte(tbCategoryPrintroute.Text.Trim());
                            Globals.db.MenuCategoryRecords[0].bFilterPrintRoute = true;
                            Globals.db.MenuCategoryRecords[0].col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                            Globals.db.MenuCategoryRecords[0].bFilterVisible = true;
                            if (cmbMenuGroup.SelectedIndex!=-1)
                            {
                                Globals.db.MenuCategoryRecords[0].col_MenuGroupID = Globals.db.MenuGroupRecords[cmbMenuGroup.SelectedIndex].col_ID;
                                Globals.db.MenuCategoryRecords[0].bFilterMenuGroupID = true; 
                            }

                            await Globals.db.UpdateMenuCategoryObject(Globals.db.MenuCategoryRecords[0]);
                            // Re-init the display
                            Globals.menuItems.initMenuCategories(true);
                            Globals.menuItems.initMenuItems(true);
                            this.DialogResult = true;
                        }
                    }
                    else
                    {
                        Globals.db.MenuCategoryRecords[0].col_Active = true;
                        Globals.db.MenuCategoryRecords[0].bFilterActive = true;
                        Globals.db.MenuCategoryRecords[0].col_Name = tbMenuCategoryName.Text.Trim();
                        Globals.db.MenuCategoryRecords[0].bFilterName = true;
                        Globals.db.MenuCategoryRecords[0].col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                        Globals.db.MenuCategoryRecords[0].bFilterVisible = true;
                        if (cmbMenuGroup.SelectedIndex != -1)
                        {
                            Globals.db.MenuCategoryRecords[0].col_MenuGroupID = Globals.db.MenuGroupRecords[cmbMenuGroup.SelectedIndex].col_ID;
                            Globals.db.MenuCategoryRecords[0].bFilterMenuGroupID = true;
                        }
                        await Globals.db.UpdateMenuCategoryObject(Globals.db.MenuCategoryRecords[0]);
                        // Re-init the display
                        Globals.menuItems.initMenuCategories(true);
                        Globals.menuItems.initMenuItems(true);
                        this.DialogResult = true;
                    }
                }
                else
                {
                    // Create a new menu category with this name
                    ServiceReferenceOpenRPOS.MenuCategory mc = new ServiceReferenceOpenRPOS.MenuCategory();
                    if (tbCategoryPrintroute.Text.Trim().Length > 0)
                    {
                        if (Convert.ToInt32(tbCategoryPrintroute.Text.Trim()) > 255)
                        {
                            MessageBox.Show("Print Route value should be between 0 - 255");
                        }
                        else
                        {
                            mc.col_Name = tbMenuCategoryName.Text.Trim();
                            mc.bFilterName = true;
                            mc.col_Active = true;
                            mc.bFilterActive = true;
                            mc.col_Precedence = (short)(maxPrecedence + 1);
                            mc.bFilterPrecedence = true;
                            mc.col_PrintRoute = Convert.ToByte(tbCategoryPrintroute.Text.Trim());
                            mc.bFilterPrintRoute = true;
                            mc.col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                            mc.bFilterVisible = true;
                            if (cmbMenuGroup.SelectedIndex != -1)
                            {
                                mc.col_MenuGroupID = Globals.db.MenuGroupRecords[cmbMenuGroup.SelectedIndex].col_ID;
                                mc.bFilterMenuGroupID = true;
                            }
                            await Globals.db.AddMenuCategoryObject(mc);
                            // Re-init the display
                            Globals.menuItems.initMenuCategories(true);
                            Globals.menuItems.initMenuItems(true);
                            this.DialogResult = true;
                        }
                    }
                    else
                    {
                        mc.col_Name = tbMenuCategoryName.Text.Trim();
                        mc.bFilterName = true;
                        mc.col_Active = true;
                        mc.bFilterActive = true;
                        mc.col_Precedence = (short)(maxPrecedence + 1);
                        mc.bFilterPrecedence = true;
                        mc.col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                        mc.bFilterVisible = true;
                        if (cmbMenuGroup.SelectedIndex != -1)
                        {
                            mc.col_MenuGroupID = Globals.db.MenuGroupRecords[cmbMenuGroup.SelectedIndex].col_ID;
                            mc.bFilterMenuGroupID = true;
                        }
                        await Globals.db.AddMenuCategoryObject(mc);
                        // Re-init the display
                        Globals.menuItems.initMenuCategories(true);
                        Globals.menuItems.initMenuItems(true);
                        this.DialogResult = true;
                    }
              } 
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbMenuCategoryName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

        private void tbCategoryPrintroute_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key);
        }

        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

    }
}

